package com.F02.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.log4j.Logger;

import com.F02.actionform.F020106ActionForm;
import com.F02.dao.F020106DAO;
import com.dao.common.DbOperationExt;
import com.dbtools.Dispose;
import com.exception.SelectException;
import com.info.T_S_YHCZXXinfo;
import com.util.CommonTools;

/**
 * @leip
 * @家族代码维护
 * @数据库操作类
 * @方法命名规则：静态参数名称、例如：查询方法，方法名 QUERYLIST();
 * @数据库某字段如果的新增值，如果要取当前时间，请在数据库中表时间的字段上设置GETDATE(), @ insert语句执行时，会自动插入时间。避免系统时间于服务器时间不统一造成错误。 @ 例表c_customers的customer_date字段。

 */

public class F020106DAO extends DbOperationExt{
	
	/**
	 * 记录操作日志;
	 */
	Logger log = Logger.getLogger(F020106DAO.class);

	/**
	 * 构造方法获取数据源;
	 * 
	 */
	public F020106DAO() {
		super();
	}

	public F020106DAO(Connection conn) {
		super(conn);
	}

	/**
	 * @查询
	 */
	public List QUERYLIST(F020106ActionForm actionForm,
			HttpServletRequest request) {
		StringBuffer msql = new StringBuffer();
		StringBuffer condition = new StringBuffer();
		/**判断查询条件，拼装查询SQL*/
		/**产品类别*/
		if(actionForm.getModelCode()!=null&&!actionForm.getModelCode().equals("")){
			condition.append(" and a.model_code = '"+actionForm.getModelCode()+"' ");
		}
		if(actionForm.getCodeFrom()!=null&&!actionForm.getCodeFrom().equals("")){
			condition.append(" and a.family_code >= '"+CommonTools.null2String(actionForm.getCodeFrom())+"' ");
		}
		if(actionForm.getCodeTo()!=null&&!actionForm.getCodeTo().equals("")){
			condition.append(" and a.family_code <= '"+CommonTools.null2String(actionForm.getCodeTo())+"' ");
		}
		List list = new ArrayList();
		Map map = null;
		ResultSet rest = null;
		
		msql.append(" SELECT a.id,a.family_code,b.[name],c.serial,");
		msql.append(" c.phases,a.finish_days,a.percents, ");	
		msql.append(" a.bottle_neck,a.maximum_capacity ");
		msql.append(" FROM t_family a,t_product_model b,t_critical_phases c ");
		msql.append(" where a.model_code=b.code and a.phases_code=c.code ");
		msql.append(" "+condition.toString());
		msql.append(" order by  a.family_code,c.serial ");
		try {
			log.info("t_family QUERYLIST sql is " + msql.toString());
			rest = super.executeQuery(msql.toString());
			while (rest.next()) { // 循环结果集并在map中添加结果集每行对应字段的值;
				map = new HashMap();
				map.put("id", CommonTools.null2String(rest
						.getString("id")));
				map.put("familyCode", CommonTools.null2String(rest
						.getString("family_code")));
				map.put("modelCode", CommonTools.null2String(rest
						.getString("name")));
				map.put("serial", CommonTools.null2String(rest
						.getString("serial")));
				map.put("phasesCode", CommonTools.null2String(rest
						.getString("phases")));
				map.put("finishDays", CommonTools.null2String(rest
						.getString("finish_days")));
				map.put("percent", CommonTools.null2String(rest
						.getString("percents")));
				map.put("bottleNeck", CommonTools.null2String(rest
						.getString("bottle_neck")));
				map.put("maximumCapacity", CommonTools.null2String(rest
						.getString("maximum_capacity")));
				
				list.add(map);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Dispose.dispose(rest); // 关闭rest及statement;
		}

		return list;
	}

	/**
	 * @新增保存
	 * @要记录操作，在插入后要向操作记录表也插一条语句，也就是用执行多天SQL， @ 所有用super.executeUpdate(sqlList,true)执行
	 */
	public boolean SAVE_INSERT(F020106ActionForm actionForm,Map map,
			int length) {
		boolean falg = false;
		List sqlList = new ArrayList();				
		/** 获得操作记录SQL */
		StringBuffer msql = new StringBuffer();		
		try {
			for(int i=0;i<length;i++){
				
				msql.append("INSERT INTO t_family( ");		
				msql.append("family_code,model_code, ");
				msql.append("serial,phases_code, ");
				msql.append("finish_days,percents, ");
				msql.append("bottle_neck, ");
				msql.append("maximum_capacity");
				msql.append(" )VALUES ( ");				
				msql.append("'" + CommonTools.null2String(actionForm.getFamilyCode()) + "', ");
				msql.append("'" + actionForm.getModelCode() + "', ");
				msql.append("adyh.FUN_GETSerial('" +  map.get("1"+i) + "'), ");
				msql.append("'" + map.get("1"+i) + "', ");
				msql.append("" + map.get("2"+i) + ", ");
				msql.append("" + map.get("3"+i) + ", ");
				msql.append("'" + map.get("4"+i) + "', ");
				msql.append("" + map.get("5"+i) + " )");
				sqlList.add(msql.toString());
				
				msql.delete(0, msql.length());			
			}
			falg = super.executeUpdate(sqlList, true);
			
			actionForm.setErrorFlag("0");/**成功*/
		} catch (SQLException ex) {
			if ("1".equals(String.valueOf(ex.getErrorCode()))) {
				/** ex.getErrorCode()返回一，表示主键冲突 */
				actionForm.setErrorFlag("1");/** 主键冲突! */
			} else {
				actionForm.setErrorFlag("1");/**保存失败*/
			}
		} catch (Exception e) {
			actionForm.setErrorFlag("4");/**  */
			e.printStackTrace();
			super.rollback();

		}
		super.commit();

		return falg;
	}

	/**
	 * @编辑
	 */
	public void EDIT(F020106ActionForm actionForm, HttpServletRequest request) {
		ResultSet rest = null;
		StringBuffer msql = new StringBuffer();
		msql.append(" SELECT a.id,family_code,d.[name] model_name,a.serial,");
		msql.append(" a.phases_code,finish_days,percents, ");	
		msql.append(" bottle_neck,maximum_capacity, ");
		msql.append(" b.product_code,c.[name] product_name ");
		msql.append(" FROM t_family a,t_critical_phases b ,");
		msql.append(" t_product_type c,t_product_model d ");
		msql.append(" WHERE a.phases_code=b.code and b.product_code=c.code ");
		msql.append(" and a.model_code=d.code and a.id='"
				+ CommonTools.null2String(request
						.getParameter("id").trim()) + "'");
		try {
			log.info("t_family QUERYLIST sql is " + msql.toString());
			rest = super.executeQuery(msql.toString());
			while (rest.next()) { // 循环结果集并在map中添加结果集每行对应字段的值;
				actionForm.setId(CommonTools.null2String(rest
						.getString("id")));
				actionForm.setFamilyCode(CommonTools.null2String(rest
						.getString("family_code")));
				actionForm.setModelCode(CommonTools.null2String(rest
						.getString("model_name")));
				actionForm.setSerial(CommonTools.null2String(rest
						.getString("serial")));
				actionForm.setPhasesCode(CommonTools.null2String(rest
						.getString("phases_code")));
				actionForm.setFinishDays(CommonTools.null2String(rest
						.getString("finish_days")));
				actionForm.setPercent(CommonTools.null2String(rest
						.getString("percents")));
				actionForm.setBottleNeck(CommonTools.null2String(rest
						.getString("bottle_neck")));
				actionForm.setMaximumCapacity(CommonTools.null2String(rest
						.getString("maximum_capacity")));
				actionForm.setProductCode(CommonTools.null2String(rest
						.getString("product_code")));
				actionForm.setProductType(CommonTools.null2String(rest
						.getString("product_name")));
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			Dispose.dispose(rest); // 关闭rest及statement;
		}
	}

	/**
	 * @编辑保存
	 */
	public boolean SAVE_UPDATE(F020106ActionForm actionForm,
			T_S_YHCZXXinfo t_YhczxxInfo) throws SelectException {
		boolean flag = true;
		List sqlList = new ArrayList();
		StringBuffer yhczsql = new StringBuffer();
		yhczsql = getInsZzxxSQL(t_YhczxxInfo);
		StringBuffer msql = new StringBuffer();
		msql.append(" UPDATE t_family  SET ");
		msql.append(" family_code = '" + CommonTools.null2String(actionForm.getFamilyCode())+ "', ");		
		msql.append(" serial = adyh.FUN_GETSerial('"+ CommonTools.null2String(actionForm.getPhasesCode()) + "'), ");
		msql.append(" phases_code = '"+ actionForm.getPhasesCode() + "', ");
		msql.append(" finish_days = "+ CommonTools.null2String(actionForm.getFinishDays()) + ", ");
		msql.append(" percents = "+ CommonTools.null2String(actionForm.getPercent()) + ", ");
		msql.append(" bottle_neck = '"+ CommonTools.null2String(actionForm.getBottleNeck()) + "', ");
		msql.append(" maximum_capacity = " + CommonTools.null2String(actionForm.getMaximumCapacity()) + " ");
		msql.append(" WHERE  id = '"+  actionForm.getId() + "' ");
		try {
			log.info("t_family SAVE_UPDATE sql is " + msql.toString());
			sqlList.add(msql);
			sqlList.add(yhczsql);
			flag = super.executeUpdate(sqlList, true);
			actionForm.setErrorFlag("0");

		} catch (Exception e) {
			actionForm.setErrorFlag("1");
			e.printStackTrace();
			super.rollback();

		}
		super.commit();
		return flag;
	}

	/**
	 * @删除
	 */
	public boolean DELETE(F020106ActionForm actionForm,
			T_S_YHCZXXinfo t_YhczxxInfo, HttpServletRequest request)
			throws SelectException {
		boolean flag = true;
		List sqlList = new ArrayList();
		StringBuffer yhczsql = new StringBuffer();
		yhczsql = getInsZzxxSQL(t_YhczxxInfo);		
		StringBuffer msql = new StringBuffer();
		msql.append(" DELETE FROM t_family ");
		msql.append(" WHERE ");
		msql.append(" id = "
				+ actionForm.getId().trim());		
		try {
			log.info("t_family delete sql is " + msql.toString());			
			sqlList.add(msql);
			sqlList.add(yhczsql);
			flag = super.executeUpdate(sqlList, true);
			actionForm.setErrorFlag("0");
		} catch (Exception e) {
			e.printStackTrace();
			super.rollback();
			actionForm.setErrorFlag("1");
		}
		super.commit();
		return flag;
	}	
	
}
